  function exceltoifolders(fname,dpath,opath,quer,nstr,s0,s1,freq); 
% function exceltoifolders(fname,dpath,opath,quer,nstr,s0,s1,freq); 
% 
% Excel to individual folders 
%
% Loads and excel spreadsheet 
% Looks for non-missing values in each 
% series and 
% Saves each series in the different subdirectory
% According to a pre-specified path which also uses 
% a prespecified number of characters in the names 
% loaded from excel 
%
% Allows all series to have different number of 
% observations 
%  
% Saves a matfile data.mat with two elements 
% dat   data vector 
% cal   calendar vector 
%
% Inputs 
% ========
% fname='GROWTH_MAT';                 % File name Where data stored 
% dpath='C:\MATLAB_DATA\WAEMU';        % Path where data stored 
% opath='C:\MATLAB_DATA\WAEMU';             % Path where to store individual files  
% quer= 'GDP';                       % Indicates Subfolder 
% nstr=3;                            % Extract from string 
% s0=1963;                           % Starting Date  
% s1=2003;                           % Ending Date 
% freq='A';                          % Frequency 
%
% AJ 4/09/03
% =========================================================================
freq=upper(freq); 
if freq~='A' & freq ~='Q' & freq~='M'
    error('Must provide a valid frequency'); 
end;
cuc=cd; 
cd(dpath); 
[datm,sernam]=xlsread(fname); 
cvec=datm(:,1);
cd(cuc); 
y=datm(:,2:end);
[T,n]=size(y); 
[nc1,nc2]=size(sernam);  
if nc1 > 1 | nc2 ~= (n+1) 
    error('Check the data strings'); 
end 
yname=sernam(2:end); 
clear datm sernam; 
% Check sample dates match what described above 
% =============================================
cvec2=calvec(s0,s1,freq); 
if any(cvec-cvec2 ~= 0) == 1; 
    error('Check Sample Dates'); 
end; 

fpraid([ ' Files will be saved in folder: ', quer ] ); 
R=input('Continue? (Y/N)','s'); 
if upper(R) == 'N'
    return
end
clear R; 

clear cvec2; 
ch=[cvec(1);cvec(end);]; 
ncal=length(cvec); 
cuc=cd;
jj=1; 
for jj=1:n; 
    [dat,cal]=cmiss(y(:,jj) , cvec);
    ch2=[cal(1);cal(end)];
    sname=yname{jj}; 
    if sum( ch == ch2 ) ~=2; 
        nch=length(cal); 
        fpraid('d'); 
        fpraid(['WARNING !!!! Missing Observations in ',sname]);
        fpraid( [num2str(ncal-nch) ' Missing Observations']) ;  
    end; 
    opatht=[opath,'\',sname(1:nstr),'\',quer]; 
    fpraid('e'); 
    fpraid(['Files will be saved in: ',opatht] );
    cd(opatht); 
    tt=exist('dat'); 
    if tt~=0;  
        fpraid('WARNING! File exists'); 
    end
    save data dat cal  
    cd(cuc); 
    clear sname; 
end 
cd(cuc); 